package searchteam.client.ui.widgets;

import java.util.ArrayList;
import java.util.Collection;

import searchteam.client.beansMirrors.PaperMirror;
import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.services.SearchTeamServices;
import searchteam.client.services.SearchTeamServicesAsync;
import searchteam.client.ui.callbacks.LinkPersonsToPaperCallback;
import searchteam.client.ui.callbacks.UpdatePaperCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UpdatePaperPopUp extends DialogBox {

	private PaperMirror paper;
	private Display display;
	
	private TextBox titleTextBox;
	private TextBox stateTextBox;
	private PersonListBox remainingAuthorsListBox;
	
	private Panel authorsPanel;
	
	Collection<PersonMirror> chosenPersonsMirrors;
	
	SearchTeamServicesAsync services = GWT.create(SearchTeamServices.class);
	
	private Button addAuthorButton;
	private Button validateButton;
	private Button cancelButton;
	
	public UpdatePaperPopUp(Display d, PaperMirror p) {
		
		this.paper = p;
		display = d;
		
		Grid mainPanel = new Grid(4, 2);
		
		this.setTitle("Modification de Papier");
		this.setText("Modification de Papier");
		this.setPopupPosition(100, 100);
	
		titleTextBox = new TextBox();
		titleTextBox.setText(paper.getTitle());
		
		stateTextBox = new TextBox();
		stateTextBox.setText(paper.getState());
		
		mainPanel.setWidget(0, 0, new Label("Titre : "));
		mainPanel.setWidget(0, 1, titleTextBox);
		mainPanel.setWidget(1, 0, new Label("Etat : "));
		mainPanel.setWidget(1, 1, stateTextBox);
		
		chosenPersonsMirrors = new ArrayList<PersonMirror>();
		
		for(PersonMirror m : paper.getAuthors()) {
			chosenPersonsMirrors.add(m);
		}
		
		remainingAuthorsListBox = new PersonListBox();
		
		addAuthorButton = new Button("+");
		addAuthorButton.addClickHandler(new AddAuthorClickHandler());
		
		HorizontalPanel addAuthorPanel = new HorizontalPanel();
		addAuthorPanel.add(remainingAuthorsListBox);
		addAuthorPanel.add(addAuthorButton);
		mainPanel.setWidget(2, 0, addAuthorPanel);

		authorsPanel = new VerticalPanel();
		Panel scrollableAuthorsPanel = new ScrollPanel(authorsPanel);
		scrollableAuthorsPanel.setHeight("90px");
	
		mainPanel.setWidget(2, 1, scrollableAuthorsPanel);
		
		validateButton = new Button("Valider");
		validateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				PaperMirror updatedPaper = UpdatePaperPopUp.this.paper;
				updatedPaper.setTitle(titleTextBox.getText());
				updatedPaper.setState(stateTextBox.getText());
				updatedPaper.setAuthors(chosenPersonsMirrors);
				services.updatePaper(updatedPaper, new UpdatePaperCallback());
				
				services.linkPersonsToPaper(chosenPersonsMirrors, updatedPaper, new LinkPersonsToPaperCallback());
				
				display.getMainUI().display(new PaperMirrorDisplay(display, paper));
				UpdatePaperPopUp.this.hide();
			}
		});
		
		cancelButton = new Button("Annuler");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UpdatePaperPopUp.this.hide();
			}
		});
		
		mainPanel.setWidget(3, 0, validateButton);
		mainPanel.setWidget(3, 1, cancelButton);
		
		refreshAuthors();
		
		this.add(mainPanel);
		this.show();
	}
		
	private void refreshAuthors() {
		authorsPanel.clear();
		
		for(PersonMirror personMirror : chosenPersonsMirrors) {
			HorizontalPanel authorEntry = new HorizontalPanel();
			
			Button removeAuthor = new Button("X");
			removeAuthor.addClickHandler(new RemoveAuthorClickHandler(personMirror));
			
			authorEntry.add(removeAuthor);
			authorEntry.add(new Label(personMirror.getName() + " " + personMirror.getFirstname()));
			
			authorsPanel.add(authorEntry);
		}
	}
	
	class AddAuthorClickHandler implements ClickHandler {

		public void onClick(ClickEvent event) {
			chosenPersonsMirrors.add(remainingAuthorsListBox.getSelectedPerson());
			refreshAuthors();
		}
	
	}
	
	class RemoveAuthorClickHandler implements ClickHandler {

		private PersonMirror personMirror;
		
		public RemoveAuthorClickHandler(PersonMirror personMirror) {
			this.personMirror = personMirror;
		}
		
		public void onClick(ClickEvent event) {
			chosenPersonsMirrors.remove(personMirror);
			refreshAuthors();
		}
		
	}
	
}
